/*
 * read_config.cpp
 *
 *  Created on: Mar 31, 2010
 *      Author: root
 */
#include "read_config.hpp"
#include "msgouttocontrol.hpp"
#include <string.h>


 const char* CConfig::GetValue(const char* nodeName)
{
	if(!mRootElem)
	{
		my_printf("Read boot node error\n");
	}

	TiXmlElement * pElem=mRootElem->FirstChildElement(nodeName);
	if (!pElem)
	{
		my_printf("Read node %s error\n",nodeName);
	}
	else my_printf("Read node %s ok\n",nodeName);
	printf("ok\n");
  //  printf("%s\n",pElem->GetText());
	printf("bad\n");
	return pElem->GetText();
}

 CConfig::CConfig(const char* xmlFileName)
{
 	if (mXmlConfigFile.LoadFile(xmlFileName)==false)
 	{
 		printf("load xml file bad:%s\n",xmlFileName);
 	}
 	else
 	{
 		printf("load xml file ok:%s\n",xmlFileName);
 	//	mXmlConfigFile.Print();
 	}
 	getRootElem();
}
 CConfig::~CConfig()
 {
	  my_printf("Start free xml\n");
	  mRootElem->Clear();
	  mXmlConfigFile.Clear();
	  my_printf("End free xml\n");
 }

void CConfig::getRootElem()
{
      mRootElem=0;
	  mRootElem=mXmlConfigFile.RootElement();
	  if (mRootElem==0)
		  printf("Read xml boot node error\n");
	  else
		  {
		  //   printf("Read xml boot node ok\n");
		   //printf("boot node name is %s\n",mRootElem->Value());
		  }
}

//Read from boot
 TiXmlElement* CConfig::GetElemofromboot(const char* name)
{
	 TiXmlElement* retunElem;
	 getRootElem();
	 if (mRootElem==0) return 0;
	 retunElem=mRootElem->FirstChildElement(name);
	 return retunElem;
}

 TiXmlElement* CConfig::GetElemofromElem(TiXmlElement* Elem,const char* name)
{
	 TiXmlElement* retunElem;
	 if (Elem==0) return 0;
	 retunElem=Elem->FirstChildElement(name);
	 return retunElem;
}


 u32 CConfig::convtext_(TiXmlElement* SecendChild,const char* nodename)
{
  const char * str;
  TiXmlElement* TheChild;
  TheChild=GetElemofromElem(SecendChild,nodename);
  if (TheChild==0) //not find
  {
	   return 0;
  }
  else
  {
	   str = TheChild->GetText();
	   if (str == 0)   //not text.
	   {
		   return 0;
	   }
	   else
	   {
		   return strtoul(str,'\0',10);
	   }
  }
}

 const char* CConfig::tconvtext_(TiXmlElement* SecendChild,const char* nodename)
 {
	  const char * str;
	  TiXmlElement* TheChild;
	  TheChild=GetElemofromElem(SecendChild,nodename);
	  if (TheChild==0) //not find
	  {
		   return "";
	  }
	  else
	  {
		   str = TheChild->GetText();
		   if (str == 0)   //not text.
		   {
			   return "";
		   }
		   else
		   {
			   return str;
		   }
	  }
 }
